# Copyright 2023 Specter Ops, Inc.
#
# Licensed under the Apache License, Version 2.0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.appdb.name }}
  namespace: {{ .Values.namespace }}
spec:
  replicas: {{ .Values.appdb.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.appdb.appName }}
  template:
    metadata:
      labels:
        app: {{ .Values.appdb.appName }}
    spec:
      containers:
      - name: {{ .Values.appdb.container.name }}
        image: {{ .Values.appdb.container.image }}
        env:
        - name: POSTGRES_USER
          value: "{{ .Values.appdb.container.env.POSTGRES_USER }}"
        - name: POSTGRES_PASSWORD
          value: "{{ .Values.appdb.container.env.POSTGRES_PASSWORD }}"
        - name: POSTGRES_DB
          value: "{{ .Values.appdb.container.env.POSTGRES_DB }}"
        ports:
        - containerPort: {{ .Values.appdb.container.ports.containerPort }}
        livenessProbe:
          exec:
            command:
            - pg_isready
            - -U
            - {{ .Values.appdb.livenessProbe.user }}
            - -d
            - {{ .Values.appdb.livenessProbe.database }}
            - -h
            - 127.0.0.1
            - -p
            - "{{ .Values.appdb.livenessProbe.port }}"
          initialDelaySeconds: {{ .Values.appdb.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.appdb.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.appdb.livenessProbe.timeoutSeconds }}
          failureThreshold: {{ .Values.appdb.livenessProbe.failureThreshold }}
        {{- if .Values.appdb.volumes.pvcEnabled }}
        volumeMounts:
        - name: postgres-data
          mountPath: /var/lib/postgresql/data
        {{- end }}
      {{- if .Values.appdb.volumes.pvcEnabled }}
      volumes:
      - name: postgres-data
        persistentVolumeClaim:
          claimName: {{ .Values.appdb.volumes.persistentVolumeClaim.claimName }}
      {{- end }}
